package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bean.Facilities;


public class FacilitiesDaoImpl extends BaseDao implements FacilitiesDao{

	public List<Facilities> getList(Facilities facilities) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Facilities> list = new ArrayList<>();
		
		try {
			conn = this.getConnection(); 
			String sql = "SELECT routeId,partsId,partsName,mileageNo,statusDesc from facilities where 1=1 ";
			
			if(facilities!=null&&facilities.getRouteId()!=0){
				sql = sql + " and routeId="+facilities.getRouteId();
			}
			
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Facilities f = new Facilities();
				f.setRouteId(rs.getInt(1));
				f.setPartsId(rs.getInt(2));
				f.setPartsName(rs.getString(3));
				f.setMileageNo(rs.getInt(4));
				f.setStatusDesc(rs.getString(5));
				list.add(f);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		
		return list;
	}

	public int update(Facilities facilities) {
		String sql="UPDATE facilities SET partsId=?,partsName=?,mileageNo=?,statusDesc=? WHERE routeId=?";
		Object[] params={facilities.getPartsId(),facilities.getPartsName(),facilities.getMileageNo(),facilities.getStatusDesc()};
		int count = this.executeUpdate(sql, params);
		return count;
	}

	public int add(Facilities facilities) {
		String sql="INSERT INTO facilities(partsId,partsName,mileageNo,statusDesc) VALUES(?,?,?,?)";
		Object[] params={facilities.getRouteId(),facilities.getPartsId(),facilities.getPartsName(),facilities.getMileageNo(),facilities.getStatusDesc()};
		int count = this.executeUpdate(sql, params);
		return count;
	}

	public int delete(int id) {
		String sql="DELETE FROM facilities WHERE routeId=?";
		Object[] params={id};
		int count = this.executeUpdate(sql, params);
		return count;
	}

}
